package com.wedo.mall.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.e.library.activity.EActivity;
import com.e.library.utils.EDensityUtils;
import com.e.library.utils.EUtils;
import com.wedo.mall.R;
import com.wedo.mall.activity.user.EvaluateActivity;
import com.wedo.mall.entity.GoodsList;
import com.wedo.mall.entity.GoodsRecordItem;
import com.wedo.mall.entity.OrderData;
import com.wedo.mall.util.WUrl;

import java.util.List;

/**
 * 购买记录控件
 * Created by e on 2017/7/17.
 */

public class GoodsListView extends LinearLayout {

    private int mItemHeight;

    public GoodsListView(Context context) {
        this(context,null);
    }

    public GoodsListView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public GoodsListView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mItemHeight = EDensityUtils.dip2px(context,125);
    }

    public void record(List<GoodsList> items){
        this.removeAllViews();
        this.setOrientation(VERTICAL);
        if (EUtils.isEmpty(items)){
            return;
        }

        int size = items.size();

        for (int i=0;i <size;i++){
            GoodsList item = items.get(i);
            View view = item(item,i == size-1);
            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
            this.addView(view,params);
        }
    }

    public void record(List<GoodsList> items,OrderData orderData,Context mContext){
        this.removeAllViews();
        this.setOrientation(VERTICAL);
        if (EUtils.isEmpty(items)){
            return;
        }

        int size = items.size();

        for (int i=0;i <size;i++){
            GoodsList item = items.get(i);
            View view = item2(item,i == size-1,orderData,mContext);
            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
            this.addView(view,params);
        }
    }

    /**
     * @param item
     * @param last 是否是最后一条数据，如果是最后一条数据，则隐藏灰色分割线
     * @return
     */
    private View item(GoodsList item,boolean last){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_goodslistitem,null);
        ImageView iv_goodicon = (ImageView) view.findViewById(R.id.iv_goodicon);
        TextView tv_goodname = (TextView) view.findViewById(R.id.tv_goodname);
        TextView tv_gooddescribe = (TextView) view.findViewById(R.id.tv_gooddescribe);
        TextView tv_price = (TextView) view.findViewById(R.id.tv_price);
        TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
        View v_divider = view.findViewById(R.id.v_divider);

        /**如果是最后一条数据，则隐藏灰色分割线*/
        v_divider.setVisibility(!last?VISIBLE:GONE);

        tv_goodname.setText(item.goods_name);
        tv_price.setText(String.format("RM%s",item.price));
        tv_number.setText(String.format("x%s",item.goods_number));
        tv_gooddescribe.setText(item.attr_list);
        Glide.with(getContext()).load(WUrl.url(item.goods_image)).error(R.drawable.place_holder_goods).placeholder(R.drawable.place_holder_goods).diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_goodicon);

        return view;
    }

    /**
     * 加评价
     * @param item
     * @param last
     * @return
     */
    private View item2(final GoodsList item, boolean last, final OrderData orderData, final Context mContext){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_goodslistitem,null);
        ImageView iv_goodicon = (ImageView) view.findViewById(R.id.iv_goodicon);
        TextView tv_goodname = (TextView) view.findViewById(R.id.tv_goodname);
        TextView tv_gooddescribe = (TextView) view.findViewById(R.id.tv_gooddescribe);
        TextView tv_price = (TextView) view.findViewById(R.id.tv_price);
        TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
        View v_divider = view.findViewById(R.id.v_divider);
        Button btn_evaluate = (Button) view.findViewById(R.id.btn_evaluate);

        /**如果是最后一条数据，则隐藏灰色分割线*/
        v_divider.setVisibility(!last?VISIBLE:GONE);

        tv_goodname.setText(item.goods_name);
        tv_price.setText(String.format("RM%s",item.price));
        tv_number.setText(String.format("x%s",item.goods_number));
        tv_gooddescribe.setText(item.attr_list);
        Glide.with(getContext()).load(WUrl.url(item.goods_image)).error(R.drawable.place_holder_goods).placeholder(R.drawable.place_holder_goods).diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_goodicon);
        if(orderData.status.equals("3") && item.is_comment.equals("0")){
            btn_evaluate.setVisibility(View.VISIBLE);
            btn_evaluate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EvaluateActivity.toEvaluate((EActivity) mContext,orderData,item);
                }
            });
        }else{
            btn_evaluate.setVisibility(View.GONE);
            btn_evaluate.setOnClickListener(null);
        }


        return view;
    }
}
